package com.traffic.api.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

/**
 * Module ID: </br>
 * Comments：本类为Properties文件管理类</br>
 * Create Date: 2011-04-07
 * 
 * @author B_JIANQIANG
 */
public class PropertyUtils {

	private static Properties property = null;

	private static final String[] FILE_NAMES = new String[] { "Weather", "SMS", "Uri", "RongCloud", "JPush",
			"JPushUser" };

	private static final String CONFIG_SUFFIX = ".properties";

	private static String PROP_FILE_PATH = "/properties/";

	static {
		if (property == null) {
			loadFiles();
		}
	}

	public static void loadFiles() {
		for (String fileName : FILE_NAMES) {
			loadFile(true, fileName + CONFIG_SUFFIX);
		}
	}

	/**
	 * load property file(default property file is "/util.properties")
	 * 
	 * @param flag
	 * 
	 */
	public static void loadFile(boolean flag, String _fileName) {
		// logger.debug("load property file is ---" + propertyFile);
		InputStream inStream = null;
		property = new Properties();
		try {
			try {
				inStream = new FileInputStream(getOuterPropertiesFile(_fileName));
			} catch (FileNotFoundException e) {
				// logger.error(e.getMessage(), e);
				System.out.println("load properties file: " + PROP_FILE_PATH + _fileName);
				inStream = PropertyUtils.class.getResourceAsStream(PROP_FILE_PATH + _fileName);
			}
			property.clear();
			property.load(inStream);

			printAll();
		} catch (Exception e) {
			if (flag) {
				loadFile(false, _fileName);
			} else {
				e.printStackTrace();
			}
		} finally {
			try {
				if (inStream != null) {
					inStream.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public static File getOuterPropertiesFile(String _fileName) {
		File file = new File("");
		File propertiesFile = new File(file.getAbsolutePath() + File.separator + PROP_FILE_PATH + _fileName);
		return propertiesFile;
	}

	private static void printAll() {
		if (property != null && property.entrySet() != null) {
			Iterator<Entry<Object, Object>> iterator = property.entrySet().iterator();
			while (iterator.hasNext()) {
				Entry<Object, Object> one = iterator.next();
				String key = (String) one.getKey();
				String value = (String) one.getValue();
				System.out.println("***PropertiesUtil key:" + key + ", value:" + value);
			}
		}

	}

	/**
	 * get value from property file
	 * 
	 * @param key
	 * @return String
	 */
	public static String getProperty(String key, String defval) {
		String result = Utils.encodeStr(Utils.trimStr(property.getProperty(key)), System.getProperty("file.encoding"));
		if (StringUtils.isEmpty(result)) {
			return defval;
		} else {
			return result;
		}
	}

	public static String getProperty(String key) {
		String result = Utils.encodeStr(Utils.trimStr(property.getProperty(key)), System.getProperty("file.encoding"));
		if (StringUtils.isEmpty(result)) {
			return "";
		} else {
			return result;
		}
	}

	public static Boolean getBooleanProperty(String key, boolean defval) {
		String result = Utils.encodeStr(Utils.trimStr(property.getProperty(key)), System.getProperty("file.encoding"));
		if (StringUtils.isEmpty(result)) {
			return defval;
		} else {
			return Boolean.valueOf(result);
		}
	}

	public static int getIntProperty(String key, int defval) {
		String result = Utils.encodeStr(Utils.trimStr(property.getProperty(key)), System.getProperty("file.encoding"));
		if (StringUtils.isEmpty(result)) {
			return defval;
		} else {
			return Integer.valueOf(result);
		}
	}

	/**
	 * set property to sort to property file
	 * 
	 * @param key
	 * @param value
	 */
	public static void setProperty(String _fileName, String key, String value) {
		FileOutputStream fos = null;
		property.setProperty(key, value);
		try {
			// 通过线程拿到当前的路径
			String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
			// 进行编码转换
			String decodePath = URLDecoder.decode(path, "utf-8");
			// System.out.println("----------- : "+decodePath);
			fos = new FileOutputStream(decodePath + _fileName);
			property.store(fos, null);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @return Returns the property.
	 */
	public static Properties getProperty() {
		return property;
	}

	/**
	 * @param property
	 *            The property to set.
	 */
	public static void setProperty(Properties property) {
		PropertyUtils.property = property;
	}

	/*
	 * public static String getPropertyFile() { return propertyFile; }
	 */
	/**
	 * @param propertyFile
	 *            The propertyFile to set.
	 */
/*	public static void setPropertyFile(String propertyFile) {
		if (!PropertyUtils.propertyFile.equals(propertyFile)) {
			PropertyUtils.propertyFile = propertyFile;
			PropertyUtils.loadFile(true);
		}
	}*/

	public static void main(String[] args) {
		PropertyUtils.getProperty("masterSecret");
	}
}
